package com.senseuni.da.ui.dialog;

import android.app.TimePickerDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.elvishew.xlog.XLog;
import com.senseuni.common.dialog.CBDialogBuilder;
import com.senseuni.common.dialog.OnDialogResultListener;
import com.senseuni.da.R;
import com.senseuni.da.utils.SpUtil;

import androidx.appcompat.widget.SwitchCompat;

public class SleepSettingDialog extends CBDialogBuilder {



    public SleepSettingDialog(Context context) {
        super(context, CBDialogBuilder.DIALOG_STYLE_NORMAL, (float) 0.5);
        initView(context);
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_sleep_setting, null);
        boolean enabled = SpUtil.INSTANCE.isSleepEnabled();

        LinearLayout layoutSleepTime = view.findViewById(R.id.ll_sleep_time);
        LinearLayout layoutActiveTime = view.findViewById(R.id.ll_active_time);
        LinearLayout layoutWeekly = view.findViewById(R.id.ll_weekly);

        layoutSleepTime.setEnabled(enabled);
        layoutActiveTime.setEnabled(enabled);
        layoutWeekly.setEnabled(enabled);

        TextView sleepTimeView = view.findViewById(R.id.tv_sleep_time);
        TextView activeTimeView = view.findViewById(R.id.tv_active_time);
        String sleepTime = SpUtil.INSTANCE.getSleepTime();
        String activeTime = SpUtil.INSTANCE.getActiveTime();
        sleepTimeView.setText(sleepTime);
        activeTimeView.setText(activeTime);

        SwitchCompat switchCompat = view.findViewById(R.id.switch_compat);
        switchCompat.setChecked(enabled);

        layoutSleepTime.setOnClickListener(v -> {
            int sleepHour = Integer.parseInt(sleepTime.split(":")[0]);
            int sleepMinute = Integer.parseInt(sleepTime.split(":")[1]);
            TimePickerDialog dialog = new TimePickerDialog(context, R.style.TimePickerDialogStyle, (view1, hourOfDay, minute) -> {
                XLog.d("hour:" + hourOfDay + ",minute:" + minute);
                sleepTimeView.setText(String.format("%02d:%02d", hourOfDay, minute));
            },
                    sleepHour,
                    sleepMinute,
                    true);
            dialog.show();
        });

        layoutActiveTime.setOnClickListener(v -> {
            int activeHour = Integer.parseInt(activeTime.split(":")[0]);
            int activeMinute = Integer.parseInt(activeTime.split(":")[1]);
            TimePickerDialog dialog = new TimePickerDialog(context, R.style.TimePickerDialogStyle, (view1, hourOfDay, minute) -> {
                XLog.d("hour:" + hourOfDay + ",minute:" + minute);
                activeTimeView.setText(String.format("%02d:%02d", hourOfDay, minute));
            },
                    activeHour,
                    activeMinute,
                    true);
            dialog.show();
        });

        TextView sundayView = view.findViewById(R.id.tv_sunday);
        sundayView.setSelected(true);
        sundayView.setOnClickListener(v -> {
            sundayView.setSelected(!sundayView.isSelected());
        });

        TextView mondayView = view.findViewById(R.id.tv_monday);
        mondayView.setSelected(true);
        mondayView.setOnClickListener(v -> {
            mondayView.setSelected(!mondayView.isSelected());
        });

        TextView tuesdayView = view.findViewById(R.id.tv_tuesday);
        tuesdayView.setSelected(true);
        tuesdayView.setOnClickListener(v -> {
            tuesdayView.setSelected(!tuesdayView.isSelected());
        });

        TextView wednesdayView = view.findViewById(R.id.tv_wednesday);
        wednesdayView.setSelected(true);
        wednesdayView.setOnClickListener(v -> {
            wednesdayView.setSelected(!wednesdayView.isSelected());
        });

        TextView thursdayView = view.findViewById(R.id.tv_thursday);
        thursdayView.setSelected(true);
        thursdayView.setOnClickListener(v -> {
            thursdayView.setSelected(!thursdayView.isSelected());

        });

        TextView fridayView = view.findViewById(R.id.tv_friday);
        fridayView.setSelected(true);
        fridayView.setOnClickListener(v -> {
            fridayView.setSelected(!fridayView.isSelected());
        });

        TextView saturdayView = view.findViewById(R.id.tv_saturday);
        saturdayView.setSelected(true);
        saturdayView.setOnClickListener(v -> {
            saturdayView.setSelected(!saturdayView.isSelected());

        });

        switchCompat.setOnCheckedChangeListener((buttonView, isChecked) -> {
            SpUtil.INSTANCE.saveSleepEnabled(isChecked);
            layoutSleepTime.setEnabled(isChecked);
            layoutActiveTime.setEnabled(isChecked);
            layoutWeekly.setEnabled(isChecked);
            sundayView.setEnabled(isChecked);
            mondayView.setEnabled(isChecked);
            tuesdayView.setEnabled(isChecked);
            wednesdayView.setEnabled(isChecked);
            thursdayView.setEnabled(isChecked);
            fridayView.setEnabled(isChecked);
        });

        setView(view);
        setTitle(context.getString(R.string.sleep_set_title));
        setTouchOutSideCancelable(false);
        showCancelButton(true);
        setButtonClickListener(false, (context1, dialog, whichBtn) -> {
            if (whichBtn == OnDialogBtnClickListener.BUTTON_CONFIRM) {
                if (mListener != null) {
                    SpUtil.INSTANCE.saveSleepTime(sleepTimeView.getText().toString());
                    SpUtil.INSTANCE.saveActiveTime(activeTimeView.getText().toString());
                    mListener.onResult(dialog, 0L);
                    dialog.dismiss();
                }
            } else if (whichBtn == OnDialogBtnClickListener.BUTTON_CANCEL) {
                dialog.dismiss();
            }
        });
    }

    private OnDialogResultListener<Long> mListener;

    public void setOnResultListener(OnDialogResultListener<Long> listener) {
        mListener = listener;
    }


}
